package sdk.org.apache.shiro.G2_使用.C09_缓存_Realm缓存_无效;

import java.util.HashMap;
import java.util.Map;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import com.titan.core.utils.data.StringUtils;
import com.titan.core.utils.io.ConsoleUtils;

import sdk.org.apache.shiro.utils.ShiroUtils;


public class UserRealm extends AuthorizingRealm {
    private final static Map<String,String> users = new HashMap<String,String>();
    
    public void putAccount(String username, String password) {
        users.put(username, password);
    }
    
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        ConsoleUtils.method(UserRealm.class, "doGetAuthorizationInfo", "username", ShiroUtils.getUsername(principals, this));
        return null;
    }
    
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String username = ShiroUtils.getUsername(token);
        String password = ShiroUtils.getPassword(token);
        ConsoleUtils.method(UserRealm.class, "doGetAuthorizationInfo", "username", username, "password", password);
        
        String passwordDB = users.get(username);
//        if(StringUtils.isNotEmpty(passwordDB) && passwordDB.equals(password)) {
//            password = ShiroUtils.getPassword(username, password);
//            SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
//                    username,  // 用户名
//                    password,  // 密码
//                    ByteSource.Util.bytes(username),
//                    getName()  // realm name
//            );
//            return authenticationInfo;
//        }
//        throw new UnknownAccountException();

        password = ShiroUtils.getPassword(username, passwordDB);
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
                username,  // 用户名
                password,  // 密码
                ByteSource.Util.bytes(username),
                getName()  // realm name
        );
        return authenticationInfo;
    
    
    }

    
    @Override
    public void clearCachedAuthorizationInfo(PrincipalCollection principals) {
        super.clearCachedAuthorizationInfo(principals);
    }

    @Override
    public void clearCachedAuthenticationInfo(PrincipalCollection principals) {
        super.clearCachedAuthenticationInfo(principals);
    }

    @Override
    public void clearCache(PrincipalCollection principals) {
        super.clearCache(principals);
    }

    public void clearAllCachedAuthorizationInfo() {
        getAuthorizationCache().clear();
    }

    public void clearAllCachedAuthenticationInfo() {
        getAuthenticationCache().clear();
    }

    public void clearAllCache() {
        clearAllCachedAuthenticationInfo();
        clearAllCachedAuthorizationInfo();
    }

}
